package com.insta.android.core;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.insta.android.activities.R;
import com.insta.android.model.MusicAssoc;

public class AssocListViewAdapteur extends BaseAdapter {

	MusicAssoc[] musicAssocs;
	Context context;

	public AssocListViewAdapteur(MusicAssoc[] musicAssocs, Context context) {
		super();
		this.musicAssocs = musicAssocs;
		this.context = context;
	}

	@Override
	public int getCount() {

		return musicAssocs.length;
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return musicAssocs[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
		View rowView = inflater.inflate(R.layout.rowplaylistassoc, parent, false);
		
		final MusicAssoc musicAssoc = musicAssocs[position];		
		ImageView image =  (ImageView) rowView.findViewById(R.id.img_playlist);	
		TextView titre = (TextView) rowView.findViewById(R.id.titre);
		TextView artist = (TextView) rowView.findViewById(R.id.artiste);
		CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.checkbox);
		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				musicAssoc.setSelected(isChecked);
			}
		});
		
		image.setImageResource(R.drawable.playlist_icone);
		titre.setText(musicAssoc.get_titre());
		artist.setText(musicAssoc.get_artiste());
		checkbox.setChecked(musicAssoc.isSelected());
				
		return rowView;
	}

	public MusicAssoc[] getMusicAssocs() {
		return musicAssocs;
	}

	public void setMusicAssocs(MusicAssoc[] musicAssocs) {
		this.musicAssocs = musicAssocs;
	}

	public Context getContext() {
		return context;
	}
	public void setContext(Context context) {
		this.context = context;
	}
}
